package com.gxa.modules.pay.service.Impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.gxa.modules.pay.mapper.PayMapper;
import com.gxa.modules.pay.service.PayService;
import com.gxa.modules.sys.dto.PayOrderDto;
import com.gxa.modules.sys.entity.Combo;
import com.gxa.modules.sys.entity.PayOrder;
import com.gxa.modules.sys.mapper.ComboMapper;
import com.gxa.modules.sys.mapper.UserMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.util.Map;

@Service
public class PayServiceImpl extends ServiceImpl<PayMapper, PayOrder> implements PayService {

    @Autowired
    public ComboMapper comboMapper;

    @Autowired
    public UserMapper userMapper;

    @Override
    public void addOrder(PayOrder payOrder) {
        this.baseMapper.insert(payOrder);
    }

    @Override
    public PayOrder queyrOrder(String orderNum) {

        PayOrder payOrder = this.baseMapper.selectOne(new QueryWrapper<PayOrder>().eq("order_num", orderNum));
        return payOrder;
    }

    @Transactional
    @Override
    public void updateState(Map<String, Object> params) {

        String orderNum = (String) params.get("orderNum");

        String com = (String) params.get("comboId");
        Integer comboId = Integer.valueOf(com);

        String us = (String) params.get("userId");
        Integer userId = Integer.valueOf(us);

        PayOrder payOrder = new PayOrder();
        payOrder.setOrderNum(orderNum);
        payOrder.setState(1);

        this.baseMapper.updateState(payOrder); //订单状态修改

        Combo combo = this.comboMapper.queryById(comboId);  //查询购套餐

        BigDecimal recharge = combo.getRecharge();

        BigDecimal sun = recharge.add(combo.getGive());  //算出总和

        this.userMapper.updateAccountById(userId,sun);  //添加余额




    }
}
